package HLLib.base;

import java.util.Enumeration;
import java.util.Hashtable;

import HLCode.HLClass;
import HLCode.HLClassManager;
import HLCode.HLClassManager_H;
import HLCode.HLLibObject;
import HLLib.io.HLFile;
import HLLib.io.HLFile_H;

public class HLCollection extends HLLibObject implements HLFile_H {
    public HLClass GetClass(HLClassManager classManager) {
        return classManager.GetLibClass(HLClassManager_H.CLASS_GROUP_TYPE_LIB_BASE, HLClassManager_H.LIB_CLASS_BASE_Collection);
    }

    Hashtable hashtable = new Hashtable();

    public void PutInteger(HLString key, int value) {
        hashtable.put(key.string, HLString.ValueOfInt(value));
    }

    public void PutString(HLString key, HLString value) {
        hashtable.put(key.string, value);
    }

    public void PutByteList(HLString key, HLByteList value) {
        StringBuffer text = new StringBuffer();
        for (int i = 0; i < value.Count(); i++) {
            if (i > 0) {
                text.append((char) 1);
            }
            text.append(value.items[i]);
        }

        hashtable.put(key.string, new HLString(text.toString()));
    }

    public void PutIntList(HLString key, HLIntList value) {
        StringBuffer text = new StringBuffer();
        for (int i = 0; i < value.Count(); i++) {
            if (i > 0) {
                text.append((char) 1);
            }
            text.append(value.items[i]);
        }

        hashtable.put(key.string, new HLString(text.toString()));
    }

    public void PutStringList(HLString key, HLList value) {
        StringBuffer text = new StringBuffer();
        for (int i = 0; i < value.Count(); i++) {
            if (i > 0) {
                text.append((char) 1);
            }
            text.append(((HLString) value.items[i]).string);
        }

        hashtable.put(key.string, new HLString(text.toString()));
    }

    public int GetInteger(HLString key) {
        try {
            return Integer.valueOf(GetString(key).string).intValue();
        } catch (Exception e) {
            return 0;
        }
    }

    public HLString GetString(HLString key) {
        return hashtable.containsKey(key.string) ? (HLString) hashtable.get(key.string) : new HLString();
    }

    public HLByteList GetByteList(HLString key) {
        HLList words = GetStringList(key);
        HLByteList bytes = new HLByteList(words.Count());
        for (int i = 0; i < words.Count(); i++) {
            try {
                bytes.items[i] = (byte) Integer.valueOf(((HLString) words.items[i]).string).intValue();
            } catch (Exception e) {
                bytes.items[i] = 0;
            }
        }
        return bytes;
    }

    public HLIntList GetIntList(HLString key) {
        HLList words = GetStringList(key);
        HLIntList ints = new HLIntList(words.Count());
        for (int i = 0; i < words.Count(); i++) {
            try {
                ints.items[i] = Integer.valueOf(((HLString) words.items[i]).string).intValue();
            } catch (Exception e) {
                ints.items[i] = 0;
            }
        }
        return ints;
    }

    public HLList GetStringList(HLString key) {
        if (hashtable.containsKey(key.string)) {
            HLString value = GetString(key);
            if (value.Length() > 0) {
                return value.Split((char) 1);
            }
        }
        return new HLList();
    }

    public void Clear() {
        hashtable.clear();
    }

    public void Remove(HLString key) {
        hashtable.remove(key.string);
    }

    public void ReadFrom(HLFile file) {
        hashtable = new Hashtable();

        try {
            int count = file.ReadInt();
            for (int i = 0; i < count; i++) {
                HLString key = file.ReadString();
                hashtable.put(key.string, file.ReadString());
            }
        } catch (Exception e) {
        }
    }

    public void ReadFromOld(HLFile file) {
        ReadFrom(file);
    }

    public void WriteTo(HLFile file) {
        file.WriteInt(hashtable.size());

        Enumeration e = hashtable.keys();
        while (e.hasMoreElements()) {
            String itemName = (String) e.nextElement();

            file.WriteString(new HLString(itemName));
            file.WriteString((HLString) hashtable.get(itemName));
        }
    }

    public boolean ContainsKey(HLString key) {
        return hashtable.containsKey(key.string);
    }

    public HLList GetKeys() {
        HLList keys = new HLList();
        Enumeration eKeys = hashtable.keys();

        while (eKeys.hasMoreElements()) {
            keys.Add(new HLString((String) eKeys.nextElement()));
        }
        return keys;
    }
}
